import {AsyncStorage} from 'react-native';

export const arrMood = [require('./assets/a.png'),require('./assets/b.png'),require('./assets/c.png'),require('./assets/d.png'),require('./assets/e.png')];

export default class DataHandler {
    static diaryList = [];
    
    static getAllTheDiary() {
        return new Promise( (resolve, reject) => {
                AsyncStorage.getAllKeys().then(
                    (Keys) => {
                        if(Keys.length === 0) {
                            resolve([]);
                            console.log('注意，resolve后的语句还会被执行，因此结束处理必须跟return语句');
                            return;
                        }
                        //否则如果不为空
                        AsyncStorage.multiGet(Keys).then(
                            (results) => {
                                DataHandler.diaryList.clear
                                const resultsLength = results.length;
                                for(let counter = 0; counter < resultsLength; ++counter) {
                                    DataHandler.diaryList[counter] = JSON.parse(results[counter][1]);
                                }
                                resolve(DataHandler.diaryList);
                            }
                        ).catch((error) => {
                            console.log(error);
                        });
                    }
                ).catch((error) => {
                    console.log(error);
                });
            }
        );
    }

    // static bubleSortDiaryList() {
    //     const length = DataHandler.diaryList.length;
    //     let tempObj;
    //     for(let i = 0; i < length; i++) {
    //         for(let j = 0; j < length - i - 1; j++) {
    //             if(DataHandler.diaryList[j].ID > DataHandler.diaryList[j+1].ID) {
    //                 tempObj = DataHandler.diaryList[j];
    //                 DataHandler.diaryList[j] = DataHandler.diaryList[j+1];
    //                 DataHandler.diaryList[j+1] = tempObj;
    //             }
    //         }
    //     }
    // }

    static getPreviousDiary() {
        // if(DataHandler.listIndex === 0) return null;
        // DataHandler.listIndex--;
        // return DataHandler.getObjByIndex(DataHandler.listIndex);
    }

    static getNextDiary() {
        // if(DataHandler.listIndex === DataHandler.diaryList.length - 1) return null;
        // DataHandler.listIndex++;
        // return DataHandler.getObjByIndex(DataHandler.listIndex);
    }

    static getObjByIndex(index) {
        // const obj = DataHandler.diray[index];
        // const ctime = new Date(obj.time);
        // const strTime = `${ctime.getFullYear()}年${ctime.getMonth()+1}月${ctime.getDate()}日 星期${ctime.getDay()+1} ${ctime.getHours()}:${ctime.getMinutes()}`;
        // const rValue = {
        //     diaryMood: arrMood[obj.mood],
        //     diaryTime: strTime,
        //     diaryTitle: obj.title,
        //     diaryBody: obj.body
        // }
        // return rValue;
    }

    static saveDiaryAndReturn(mood, body, title) {
        return new Promise((resolve, reject) => {
            const ctime = new Date();
            const strTime = `${ctime.getFullYear()}年${ctime.getMonth()+1}月${ctime.getDate()}日 星期${ctime.getDay()} ${ctime.getHours()}:${ctime.getMinutes()}`;
            let aDiary = Object();
            aDiary.title = title;
            aDiary.mood = mood;
            aDiary.time = strTime;
            aDiary.body = body;
            aDiary.sectionID = `${ctime.getFullYear()}年${ctime.getMonth()+1}月`;
            aDiary.ID = Date.parse(ctime);

            AsyncStorage.setItem(`${aDiary.ID}`, JSON.stringify(aDiary))
                .then(() => {
                    const newIndex = DataHandler.diaryList.length;
                    DataHandler.diaryList[newIndex] = aDiary;
                    resolve(DataHandler.diaryList);
                }).catch((error) => {
                    console.log(`Saving failed, error：${error.message}`);
                });
        });
    }
}